package com.stable.actor.remotedemo;

import com.stable.actor.core.ActorRef;
import com.stable.actor.core.ActorSystem;
import com.stable.actor.core.Props;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class SimpleLocalDemoServer {

    public static void main(String[] args) throws InterruptedException {
        log.info("🚀 启动服务器端...");

        // 创建 ActorSystem，使用单一端口（例如 2551）
        ActorSystem system = new ActorSystem("ServerSystem");

        try {
            // 第一步：创建服务Actor - 不再需要指定独立端口
            log.info("🔧 创建服务Actor（使用系统单一端口）...");

            ActorRef paymentService = system.actorOf(
                    Props.create(() -> new PaymentServiceActor()),
                    "payment-service"  // 不再需要端口参数
            );

            ActorRef inventoryService = system.actorOf(
                    Props.create(() -> new InventoryServiceActor()),
                    "inventory-service"  // 不再需要端口参数
            );

            ActorRef notificationService = system.actorOf(
                    Props.create(() -> new NotificationServiceActor()),
                    "notification-service"  // 不再需要端口参数
            );

            // 创建订单服务
            OrderServiceActor orderActor = new OrderServiceActor();
            ActorRef orderService = system.actorOf(Props.create(() -> orderActor), "order-service");

            // 设置订单服务的依赖
            orderActor.setRemoteServices(paymentService, inventoryService, notificationService);

            // 打印状态确认
            system.printActorStatus();


            log.info("✅ 服务器启动完成，系统端口: {}", system.getSystemPort());
            log.info("📡 服务地址:");
            log.info("  - Payment: {}:{}", "localhost", system.getSystemPort());
            log.info("  - Inventory: {}:{}", "localhost", system.getSystemPort());
            log.info("  - Notification: {}:{}", "localhost", system.getSystemPort());
            log.info("  - Order: {}:{}", "localhost", system.getSystemPort());

            // 等待客户端连接
            log.info("⏳ 等待客户端连接...");
            while (!system.isTerminated()) {
                Thread.sleep(1000);
            }

        } finally {
            log.info("\n🛑 关闭服务器系统...");
            system.shutdown();
        }

        log.info("🎉 服务器测试完成!");
    }
}
